# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from tortoise import fields
from kernels.model import DbModel


class DrawsPromptCategoryModel(DbModel):
    id = fields.IntField(pk=True, description="主键")
    engine = fields.CharField(null=False, max_length=20, description="绘图引擎: [mj,sd,dalle,doubao]")
    name = fields.CharField(null=False, max_length=100, description="分类名称")
    sort = fields.IntField(null=False, default=0, description="排序编号")
    is_disable = fields.SmallIntField(null=False, default=0, description="是否禁用: [0=否, 1=是]")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "绘画分类表"
        table = DbModel.table_prefix("draws_prompt_category")

    @classmethod
    async def queryCateDictsById(cls, ids: list) -> dict:
        """ 获取分类字典 """
        dicts = {}
        if ids:
            field = ["id", "name"]
            lists = await (cls.filter(id__in=list(set(ids))).all().values(*field))
            dicts = {item["id"]: item["name"] for item in lists}
        return dicts


class DrawsPromptExampleModel(DbModel):
    id = fields.IntField(pk=True, auto_increment=True, description="ID")
    engine = fields.CharField(null=False, max_length=20, description="绘图引擎: [mj,sd,dalle,doubao]")
    title = fields.CharField(null=False, max_length=100, description="标题")
    content = fields.TextField(description="内容")
    sort = fields.IntField(null=False, default=0, description="排序")
    is_disable = fields.BooleanField(null=False, description="是否禁用: [0=否, 1=是]")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "绘画示例表"
        table = DbModel.table_prefix("draws_prompt_example")


class DrawsPromptLexiconModel(DbModel):
    id = fields.IntField(pk=True, auto_increment=True, description="ID")
    engine = fields.CharField(null=False, max_length=20, description="绘图引擎: [mj,sd,dalle,doubao]")
    category_id = fields.IntField(null=False, default=0, description="分类ID")
    prompt = fields.CharField(null=False, max_length=250, description="中文提示词")
    prompt_en = fields.CharField(null=False, max_length=250, description="英文提示词")
    sort = fields.IntField(null=False, default=0, description="排序")
    is_disable = fields.BooleanField(null=False, description="是否禁用: [0=否, 1=是]")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "绘画词汇表"
        table = DbModel.table_prefix("draws_prompt_lexicon")


class DrawsRecordsModel(DbModel):
    id = fields.IntField(pk=True, description="主键")
    user_id = fields.IntField(null=False, default=0, description="用户ID")
    code = fields.CharField(null=False, default="", max_length=32, description="记录编号")
    engine = fields.CharField(null=False, default="", max_length=32, description="绘图引擎: [mj,sd,dalle,doubao]")
    type = fields.CharField(null=False, default="", max_length=32, description="绘图类型: [text2image,image2image]")
    action = fields.CharField(null=False, default="", max_length=32, description="绘图动作: [upscale1,variation1]")
    model = fields.CharField(null=False, default="", max_length=200, description="绘画模型")
    scale = fields.CharField(null=False, default="", max_length=100, description="图片比例")
    version = fields.CharField(null=False, default="", max_length=250, description="版本参数")
    prompt_in = fields.TextField(default="", description="输入提示词")
    prompt_en = fields.TextField(default="", description="英文提示词")
    prompt_eq = fields.TextField(default="", description="提示词详情")
    other_params = fields.TextField(default="", description="其它参数")
    complex_params = fields.TextField(default="", description="高级参数")
    thumbnail = fields.CharField(null=False, default="", max_length=250, description="缩略图地址")
    image_ref = fields.CharField(null=False, default="", max_length=250, description="参考图地址")
    image_url = fields.CharField(null=False, default="", max_length=250, description="图像的URL")
    image_loc = fields.CharField(null=False, default="", max_length=250, description="本地图地址")
    image_id = fields.CharField(null=False, default="", max_length=250, description="图像的ID")
    task_id = fields.CharField(null=False, default="", max_length=250, description="任务的ID")
    able_actions = fields.CharField(null=False, default="", max_length=250, description="操作动作")
    fail_reason = fields.TextField(default="", description="失败原因")
    notify_snap = fields.TextField(default="", description="回调快照")
    retry_count = fields.IntField(null=False, default=0, description="重试次数")
    points = fields.IntField(null=False, default=0, description="消耗积分")
    status = fields.SmallIntField(null=False, default=0, description="生成状态: [0=等待中, 1=执行中, 2=成功, 3=失败]")
    is_free = fields.SmallIntField(null=False, default=0, description="免费调用: [0=否, 1=是]")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    start_time = fields.IntField(null=False, default=0, description="开始时间")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "绘画记录表"
        table = DbModel.table_prefix("draws_records")


class DrawsSquareModel(DbModel):
    id = fields.IntField(pk=True, description="主键")
    source = fields.IntField(null=False, default=0, description="来源: [1=官方, 2=用户]")
    operate_id = fields.IntField(null=False, default=0, description="操作人ID")
    cate_id = fields.IntField(null=False, default=0, description="分类ID")
    record_id = fields.IntField(null=False, default=0, description="记录ID")
    draw_code = fields.CharField(null=False, default="", max_length=32, description="绘画编号")
    thumbnail = fields.CharField(null=False, default="", max_length=250, description="缩略图")
    image_loc = fields.CharField(null=False, default="", max_length=250, description="本地图")
    prompt_in = fields.TextField(default="", description="输入提示词")
    prompt_en = fields.TextField(default="", description="英文提示词")
    sort = fields.IntField(null=False, default=0, description="排序编号")
    public_sum = fields.IntField(null=False, default=1, description="公开次数")
    audit_result = fields.CharField(null=False, max_length=300, default="", description="审核结果")
    audit_status = fields.SmallIntField(null=False, default=0, description="审核状态: [0=待审, 1=审核通过, 2=审核拒绝]")
    is_enable = fields.SmallIntField(null=False, default=0, description="是否显示: [0=否, 1=是]")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "绘画广场表"
        table = DbModel.table_prefix("draws_square")

    @classmethod
    async def querySquareDictsByRecordId(cls, ids: list, field: list = None) -> dict:
        """ 获取广场字典 """
        dicts = {}
        if ids:
            field = cls.without_field("is_delete,delete_time") if not field else field
            result = await (cls.filter(record_id__in=set(ids), is_delete=0).all().values(*field))
            dicts = {item["record_id"]: item for item in result}
        return dicts


class DrawsCollectModel(DbModel):
    id = fields.IntField(pk=True, description="主键")
    user_id = fields.IntField(null=False, default=0, description="用户ID")
    record_id = fields.IntField(null=False, default=0, description="记录ID")
    total_num = fields.IntField(null=False, default=0, description="操作次数")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "绘画收藏表"
        table = DbModel.table_prefix("draws_collect")
